package com.example.administrator.zhongyichou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.zhongyichou.Bean.BaseEventBean;
import com.example.administrator.zhongyichou.adapter.DiseaseDetailsGridAdapter;
import com.example.administrator.zhongyichou.base.BaseActivity;
import com.example.administrator.zhongyichou.base.MyApplication;
import com.example.administrator.zhongyichou.config.Constants;
import com.example.administrator.zhongyichou.help.CallPhone;
import com.example.administrator.zhongyichou.net.NetConfig;
import com.example.administrator.zhongyichou.net.core.RequestHttpLoader;
import com.example.administrator.zhongyichou.utils.ToastUtil;
import com.example.administrator.zhongyichou.view.CircleImageView;
import com.example.administrator.zhongyichou.help.StringImageToList;
import com.example.administrator.zhongyichou.net.core.HttpLoader;
import com.example.administrator.zhongyichou.net.listener.ResultListener;
import com.example.administrator.zhongyichou.net.protocol.DiseaseDetailsProtocol;
import com.example.administrator.zhongyichou.net.protocol.GsonBaseProtocol;
import com.example.administrator.zhongyichou.view.HeaderView;
import com.example.administrator.zhongyichou.view.MyDialog;
import com.example.administrator.zhongyichou.view.MyGridView;
import com.example.administrator.zhongyichou.R;
import com.example.administrator.zhongyichou.view.PromiseDialog;
import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.preview.ImagePreviewActivity;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lichenyu on 2017/11/22.
 * 描述：我的筹款详情-梦想
 */

public class MyDreamDataActivity extends BaseActivity<DiseaseDetailsProtocol> implements AdapterView.OnItemClickListener {
    @BindView(R.id.headerview)
    HeaderView headerview;
    @BindView(R.id.txt_help_details)
    TextView txtHelpDetails;
    @BindView(R.id.txt_show_all)
    TextView txtShowAll;
    @BindView(R.id.gridview)
    MyGridView gridview;
    @BindView(R.id.txt_day)
    TextView txtDay;
    @BindView(R.id.txt_day0)
    TextView txtDay0;
    @BindView(R.id.txt_day1)
    TextView txtDay1;
    @BindView(R.id.txt_phone)
    TextView txtPhone;
    @BindView(R.id.txt_want_money)
    TextView txtWantMoney;
    @BindView(R.id.txt_money)
    TextView txtMoney;
    @BindView(R.id.txt_second)
    TextView txtSecond;
    @BindView(R.id.img_head)
    CircleImageView imgHead;
    @BindView(R.id.ll_edit)
    LinearLayout llEdit;
    @BindView(R.id.ll_cheats)
    LinearLayout llCheats;
    @BindView(R.id.ll_prove)
    LinearLayout llProve;
    @BindView(R.id.ll_delete)
    LinearLayout llDelete;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_yongtu)
    TextView txtYongtu;
    @BindView(R.id.ll_show)
    LinearLayout llShow;
    @BindView(R.id.ll_call)
    LinearLayout llCall;
    private DiseaseDetailsGridAdapter adapter;
    private List<String> list;
    private String infoID;
    private DiseaseDetailsProtocol mGsonBaseProtocal;

    @Override
    protected void init() {
        ButterKnife.bind(this);
        initHead();
    }

    @Override
    protected void responseData(DiseaseDetailsProtocol mGsonBaseProtocal) {
        this.mGsonBaseProtocal = mGsonBaseProtocal;
        txtDay.setVisibility(View.GONE);
        txtDay1.setVisibility(View.GONE);
        txtDay0.setVisibility(View.VISIBLE);
        switch (mGsonBaseProtocal.getData().getAudit_status()) {
            case "0":
                txtDay0.setText("待审核");
                break;
            case "1":
                txtDay0.setText("审核中");
                break;
            case "2":
                txtDay0.setText("审核未通过");
                break;
            case "3":
                txtDay0.setText("已拒绝");
                break;
            case "4":
                txtDay.setVisibility(View.VISIBLE);
                txtDay1.setVisibility(View.VISIBLE);
                txtDay0.setVisibility(View.VISIBLE);
                txtDay0.setText("剩余");
                txtDay.setText(mGsonBaseProtocal.getData().getLast_day() + "");
                txtDay1.setText("天");
                break;
            case "5":
                txtDay0.setText("已结束");
                break;
        }
        txtPhone.setText(mGsonBaseProtocal.getData().getInfo_tel());
        txtWantMoney.setText(mGsonBaseProtocal.getData().getAims_money() + "");
        txtMoney.setText(mGsonBaseProtocal.getData().getGot_money() + "");
        txtSecond.setText(mGsonBaseProtocal.getData().getRaise_num());
        txtHelpDetails.setText(mGsonBaseProtocal.getData().getInfo_content());
        txtTitle.setText(mGsonBaseProtocal.getData().getInfo_title());
        txtYongtu.setText(mGsonBaseProtocal.getData().getAmount_use());
        list = StringImageToList.toList(mGsonBaseProtocal.getData().getInfo_img());
        adapter = new DiseaseDetailsGridAdapter(this, list);
        gridview.setAdapter(adapter);
        gridview.setOnItemClickListener(this);
    }

    @Override
    protected HttpLoader initHttpLoader() {
        infoID = getIntent().getStringExtra("info_id");
        RequestHttpLoader requestHttpLoader = new RequestHttpLoader(DiseaseDetailsProtocol.class);
        Map<String, String> map = new HashMap<>();
        map.put("info_id", infoID);
        map.put("user_id", MyApplication.getUserID());
        requestHttpLoader.setParams(NetConfig.RELEASE_DISEASE_INFO, map);
        return requestHttpLoader;
    }

    @Override
    protected boolean isRequestData() {
        return true;
    }

    @Override
    protected int initContentView() {
        return R.layout.my_dream_data_activity;
    }

    @Override
    public boolean onEventBus(BaseEventBean baseEventBean) {
        if (baseEventBean.code == Constants.TYPE_6) {
            requestData();
        }
        return false;
    }

    private void initHead() {
        headerview.setTitle("我的筹款详情");
        headerview.setRightText("分享");
        headerview.bindOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case HeaderView.LEFT:
                        finish();
                        break;
                }
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        List<ImageInfo> imageInfo = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            ImageInfo info = new ImageInfo();
            info.bigImageUrl = list.get(i);
            info.thumbnailUrl = list.get(i);
            //拿到view的位置
            View imageView;
            imageView = gridview.getChildAt(position);
            info.imageViewWidth = imageView.getWidth();
            info.imageViewHeight = imageView.getHeight();
            int[] points = new int[2];
            imageView.getLocationInWindow(points);
            info.imageViewX = points[0];
            info.imageViewY = points[1] - getStatusBarHeight();
            imageInfo.add(info);
        }
        // 启动预览.
        Intent intent = new Intent(this, ImagePreviewActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(ImagePreviewActivity.IMAGE_INFO, (Serializable) imageInfo);
        bundle.putInt(ImagePreviewActivity.CURRENT_ITEM, position);
        intent.putExtras(bundle);
        startActivity(intent);
        overridePendingTransition(0, 0);
    }

    @OnClick({R.id.ll_edit, R.id.ll_cheats, R.id.ll_prove, R.id.ll_wallet, R.id.ll_delete, R.id.ll_show, R.id.ll_call, R.id.txt_show_all})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_show_all://显示全部
                Intent intentAll = new Intent(this, ContentActivity.class);
                intentAll.putExtra("content", mGsonBaseProtocal.getData().getInfo_content());
                startActivity(intentAll);
                break;
            case R.id.ll_edit://编辑
                if (!mGsonBaseProtocal.getData().getAudit_status().equals("0")) {
                    ToastUtil.showToast("只有待审核筹款才能进行编辑");
                    return;
                }
                Intent intent = new Intent(this, LaunchDreamFundraisingActivity.class);
                intent.putExtra("info_id", mGsonBaseProtocal.getData().getInfo_id());
                startActivity(intent);
                break;
            case R.id.ll_cheats:
                break;
            case R.id.ll_prove:
                Intent intentProve = new Intent(this, DreamDataDetailsActivity.class);
                intentProve.putExtra("info_id", mGsonBaseProtocal.getData().getInfo_id());
                startActivity(intentProve);
                break;
            case R.id.ll_wallet:
                Intent intentWallet = new Intent(this, WalletActivity.class);
                intentWallet.putExtra("info_id", infoID);
                startActivity(intentWallet);
                break;
            case R.id.ll_delete:
                MyDialog dialog = new MyDialog(this);
                dialog.setTitle("提示");
                dialog.setTxtContent("确认删除筹款？");
                dialog.setTxtConfirm("确认");
                dialog.setTxtCancel("取消");
                dialog.setOnChekedClick(new MyDialog.OnChekedClick() {
                    @Override
                    public void onCancelClick() {

                    }

                    @Override
                    public void onConfirmClick() {
                        delete();
                    }
                });
                dialog.show();
                break;
            case R.id.ll_show:
                PromiseDialog dialog1 = new PromiseDialog(this);
                dialog1.show();
                break;
            case R.id.ll_call:
                CallPhone.call(this, getString(R.string.phone));
                break;
        }
    }


    /**
     * 删除筹款
     */
    private void delete() {
        RequestHttpLoader requestHttpLoader = new RequestHttpLoader(GsonBaseProtocol.class);
        Map<String, String> map = new HashMap<>();
        map.put("user_id", MyApplication.getUserID());
        map.put("info_id", infoID);
        requestHttpLoader.setParams(NetConfig.DELETE_RAISE, map);
        requestHttpLoader.showDialog(this, true, "正在删除");
        requestHttpLoader.executor(new ResultListener() {
            @Override
            public void success(GsonBaseProtocol mGsonBaseProtocol) {
                ToastUtil.showToast("删除成功");
                //刷新界面
                EventBus.getDefault().post(new BaseEventBean<>(Constants.TYPE_4));
                finish();
            }

            @Override
            public void error(String mErrorProtocal) {

            }

            @Override
            public void notNet() {

            }

            @Override
            public void requestError() {

            }
        });
    }
}
